package com.i2863.shiroDemo.util;

/**
 * Created by xuweiwei on 2017/5/4.
 */

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cexy
 * @date 2020/11/10 14:12
 * @description:
 */
public class QiniuUtil {

    private static final CloseableHttpClient CLIENT = HttpSessionUtils.buildHttpClient();

    //获取不同站点上传域
    public static final String GET_BUCKET_URL = "******";
    public static final String ACCESS_KEY = "******";
    public static final String SECRET_KEY = "******";
    //要上传的空间
    public static final String BUCKET_NAME = "******";
    public static final String HEAD_PREFIX = "******";
    public static final String NROP_PREFIX = "******";
    public static final String NROP_SUFFIX = "?nrop";
    //密钥配置
    public static final Auth AUTH = Auth.create(ACCESS_KEY, SECRET_KEY);

    //创建上传对象
    public static UploadManager uploadManager;

    static {
        Region r = Region.autoRegion();
        Configuration c = new Configuration(r);
        uploadManager = new UploadManager(c);
    }

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public static String getUpToken(String key) {
        return AUTH.uploadToken(BUCKET_NAME, key);
    }

    public static String getImageUrl(String key) {
        return HEAD_PREFIX + key;
    }

    public static String getNropUrl(String key) {
        return NROP_PREFIX + key + NROP_SUFFIX;
    }

    public static String upload(String key, byte[] bs) throws IOException {
        try {
            //调用put方法上传
            Response res = uploadManager.put(bs, key, getUpToken(key));
            //打印返回的信息
            String body = res.bodyString();
            JsonObject object = JsonParser.parseString(body).getAsJsonObject();
            String key1 = object.get("key").getAsString();
            if (key1 == null || key1.length() <= 0) {
                throw new UploadException("无效的key:" + key1);
            }
            return key1;
        } catch (QiniuException e) {
            throw new UploadException(e);
        }
    }

    //根据站点ID获取七牛上传域
    public static Map<String, Object> getUploadManagerForSiteId(Integer siteId) throws IOException {
        Map<String, String> params = new HashMap<>(4);
        params.put("method", "orgInfo");
        params.put("siteId", siteId.toString());
        String result = HttpSessionUtils.httpPost(CLIENT, GET_BUCKET_URL, params);
        JsonObject resultObject = JsonParser.parseString(result).getAsJsonObject();
        JsonObject data = resultObject.getAsJsonObject("data");

        String token = Auth.create(data.get("ak").getAsString(), data.get("sk").getAsString()).uploadToken(data.get("bucket").getAsString());
        Region z = Region.autoRegion();
        Configuration c = new Configuration(z);
        UploadManager uploadManager = new UploadManager(c);
        String host = data.get("host").getAsString();
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("uploadManager", uploadManager);
        map.put("host", host);
        return map;
    }

    public static String bucketSiteIdUpload(Integer siteId, String filename, byte[] bs) throws IOException {
        Map<String, Object> map = getUploadManagerForSiteId(siteId);
        UploadManager uploadManager = (UploadManager) map.get("uploadManager");
        String host = (String) map.get("host");
        Response res = uploadManager.put(bs, filename, (String) map.get("token"));
        JsonObject bodyObject = JsonParser.parseString(res.bodyString()).getAsJsonObject();
        return "http://" + host + "/" + bodyObject.get("key").getAsString();
    }

/*    public static void main(String[] args) throws IOException {
        System.out.println(getUploadManagerForSiteId(127));
    }*/
}
